/**
 * 
 */
package com.cp16.tss.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.cp16.tss.model.UserLogin;

/**
 * @author ADMIN
 *
 */
public class UserLoginDAO {

	SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
	
	@SuppressWarnings("unchecked")
	public List<UserLogin> getAllUser(){
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		Criteria criteria = session.createCriteria(UserLogin.class);
		List<UserLogin> userList = criteria.list();
		session.getTransaction().commit();
		session.close();
		return userList;
	}
	
	public boolean addUser(UserLogin user) throws Exception {
		try {
			Session session = sessionFactory.openSession();
			session.beginTransaction();
			session.save(user);
			session.getTransaction().commit();
			session.close();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public boolean editUser(UserLogin user) throws Exception {
		Session session = sessionFactory.openSession();
		try {
			session.beginTransaction();
			session.update(user);
			session.getTransaction().commit();
			return true;
		} catch (HibernateException e) {
			session.getTransaction().rollback();
			e.printStackTrace();
			return false;
		} finally {
			session.close();
		}
	}

	public boolean deleteUser(UserLogin user) throws Exception {
		Session session = sessionFactory.openSession();
		if (user.isOndelete()) {
			return false;
		} else {
			session.beginTransaction();
			session.delete(user);
			session.getTransaction().commit();
			session.close();
			return true;
		}
	}
}
